<?php

namespace app\api\controller;

use app\services\WorkService;
use app\services\WorkShareService;
use support\Request;

class WorkController
{
    public function getAllCategories()
    {
        try {
            $categories = WorkService::getAllCategories();
            return ok_rsp('成功', $categories);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function share(Request $request)
    {
        try {
            $workId = $request->post('work_id');
            $inviteCodeGenerateWay = $request->post('invite_code_generate_way', 0);
            $inviteCode = $request->post('invite_code', '');
            $expireTime = $request->post('expiry_time', 0);
            $autoFill = $request->post('auto_fill', 0);

            $url = WorkShareService::generateShareLink($inviteCodeGenerateWay, $inviteCode, $expireTime, $autoFill, $workId);

            return ok_rsp('成功', ['url' => $url]);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function getShareData(Request $request)
    {
        try {
            $workId = $request->post('work_id');

            $data = WorkShareService::getShareData($workId);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function checkUrl(Request $request)
    {
        try {
            $no = $request->post('no');
            $inviteCode = $request->post('invite_code', '');

            $data = WorkShareService::checkUrl($no, $inviteCode);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function add(Request $request)
    {
        try {
            $name = $request->post('name');
            $images = $request->post('images');
            $url = $request->post('url');
            $no = $request->post('no');
            $ossName = $request->post('oss_name');
            $categoryId = $request->post('category_id', 0);

            $work = WorkService::add($name, $images, $url, $ossName, $no, $categoryId);

            return ok_rsp('成功', $work);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function list(Request $request)
    {
        try {
            $page = $request->post('page', 1);
            $length = $request->post('length', 10);
            $type = $request->post('type', 0);
            $no = $request->post('no');

            $data = WorkService::getList($page, $length, $no, $type);

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }


    public function detail(Request $request)
    {
        try {
            $id = $request->post('id');

            $work = WorkService::getDetail($id);

            return ok_rsp('成功', $work);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function edit(Request $request)
    {
        try {
            $id = $request->post('id');
            $images = $request->post('images');
            $url = $request->post('url');
            $name = $request->post('name');

            $work = WorkService::edit($id, $images, $url, $name);

            return ok_rsp('成功', $work);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function editAttr(Request $request)
    {
        try {
            $id = $request->post('id');
            $isDelete = $request->post('is_delete');
            $isPublic = $request->post('is_public');
            $price = $request->post('price');
            $name = $request->post('name');
            $categoryId = $request->post('category_id', 0);
            $tags = $request->post('tags', '');

            $work = WorkService::editAttr($id, $isPublic, $isDelete, $price, $categoryId, $tags, $name);

            return ok_rsp('成功', $work);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function getRecommendedWorks()
    {
        try {
            $works = WorkService::getRecommendedWorks();
            return ok_rsp('成功', $works);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function getWorksByCategory(Request $request)
    {
        try {
            $categoryId = $request->post('category_id', 0);
            $page = $request->post('page', 1);
            $length = $request->post('length', 12);
            $keyword = $request->post('keyword', '');

            $works = WorkService::getWorksByCategory($categoryId, $page, $length, $keyword);
            return ok_rsp('成功', $works);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function getWorkDetail(Request $request)
    {
        try {
            $id = $request->post('id');

            $work = WorkService::getWorkDetailById($id);
            return ok_rsp('成功', $work);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function purchase(Request $request)
    {
        try {
            $workId = $request->post('work_id');
            $payWay = $request->post('pay_way');
            $payWayType = $request->post('pay_way_type', 'h5');

            $res = WorkService::createOrder($payWay, $workId, $payWayType);
            return ok_rsp('成功', $res);
        } catch (\Exception $e) {
            echo $e->getMessage() . $e->getTraceAsString();
            return err_rsp($e->getMessage());
        }
    }

    public function delete(Request $request)
    {
        try {
            $id = $request->post('id');

            WorkService::delete($id);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function setPublicStatus(Request $request)
    {
        try {
            $id = $request->post('id');
            $status = $request->post('status');

            $work = WorkService::setPublicStatus($id, $status);

            return ok_rsp('成功', $work);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }
}